package com.hegongda.dao.impl;

import com.hegongda.bean.OrderItem;
import com.hegongda.bean.OrderItemView;
import com.hegongda.bean.Orders;
import com.hegongda.dao.OrdersDao;
import com.hegongda.servlet.Constr;
import com.hegongda.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    /*
     * 方法添加订单表
     * 业务层传递订单对象
     */
    public void submitOrder(Orders orders) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orders(oid,ordertime,total,state,uid) values(?,?,?,?,?)";
        Object[] params = {orders.getOid(),orders.getOrdertime(),orders.getTotal(),orders.getState(),orders.getUid()};
        qr.update(JdbcUtils.getConnection(),sql,params);
    }

    /*
     * 方法添加订单项,中间表
     * 业务层传递订单项对象
     */
    public void submitOrderItem(OrderItem orderItem) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into orderitem values(?,?,?,?)";
        Object[] params = {orderItem.getCount(),orderItem.getSubTotal(),orderItem.getPid(),orderItem.getOid()};
        qr.update(JdbcUtils.getConnection(),sql,params);
    }

    @Override
    public List<Orders> getAllOrder(String uid, int limitone, int limittwo) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT * FROM orders WHERE uid=? AND state!=4 ORDER BY ordertime DESC LIMIT ?,?";
        List<Orders> query = qr.query(sql, new BeanListHandler<Orders>(Orders.class), uid, limitone, limittwo);
        for (Orders orders : query) {
            String oid = orders.getOid();
            sql = "SELECT p.pid, p.pname, p.pimage, p.shop_price, o.count, o.subtotal FROM product  p,orderitem o WHERE p.pid = o.pid AND o.oid=?";
            List<OrderItemView> query1 = qr.query(sql, new BeanListHandler<OrderItemView>(OrderItemView.class), oid);
            orders.setOrderViewList(query1);
        }
        return query;
    }

    @Override
    public Orders getOneOrder(String oid) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from orders where oid=?";
        Orders orders =  qr.query(sql,new BeanHandler<Orders>(Orders.class),oid);
        sql="SELECT p.pid, p.pname, p.pimage, p.shop_price, o.count, o.subtotal FROM product  p,orderitem o WHERE p.pid = o.pid AND o.oid=?";
        List<OrderItemView>  oiv = qr.query(sql,new BeanListHandler<OrderItemView>(OrderItemView.class),oid);
        orders.setOrderViewList(oiv);
        return orders;
    }

    public long getOrdersCount(String uid) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select count(*) from orders where uid=? AND state!=4";
        return (Long)qr.query(sql,new ScalarHandler(),uid);
    }

    public void updateOrderState(String oid,int state) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "update orders set state=? where oid=?";
        qr.update(sql,state,oid);
    }

    @Override
    public int delOrder(String oid) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql1= "delete from orderitem where oid=?";
        String sql2= "delete from orders where oid=?";
        qr.update(sql1,oid);
        return qr.update(sql2,oid);
    }

    public void updateOrder(String name, String address, String telephone, String oid) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "update orders set name=?,address=?,telephone=? where oid=?";
        Object[] params = {
                name,address,telephone,oid
        };
        qr.update(sql,params);
    }
}